//
//  PreferenceManager.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PreferenceManager.h"

@implementation PreferenceManager


+(int)getNumTries{
    NSNumber * num = [[NSUserDefaults standardUserDefaults] objectForKey:@"num_tries"];
    if (num != nil) {
        return [num intValue];
    }
    return 0;
}

+(void)increaseNumTries{
    int tries = [PreferenceManager getNumTries];
    tries++;
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:tries] forKey:@"num_tries"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)resetNumTries{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:0] forKey:@"num_tries"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
}

+(int)appLocked{
    NSNumber * num = [[NSUserDefaults standardUserDefaults] objectForKey:@"app_locked"];
    if (num != nil) {
        return [num intValue];
    }
    return 0;
}

+(void)lockApp{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:1] forKey:@"app_locked"];
}

+(void)unlockApp{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:0] forKey:@"app_locked"];
}

+(int)config_getInterval{
    NSNumber * num = [[NSUserDefaults standardUserDefaults] objectForKey:@"config_interval"];
    if (num != nil) {
        return [num intValue];
    }
    return 5;
}

+(int)config_getChance{
    NSNumber * num = [[NSUserDefaults standardUserDefaults] objectForKey:@"config_chance"];
    if (num != nil) {
        return [num intValue];
    }
    return 5;
}

+(int)config_getLock{
    NSNumber * num = [[NSUserDefaults standardUserDefaults] objectForKey:@"config_lock"];
    if (num != nil) {
        return [num intValue];
    }
    return 60;
}

+(void)config_setInterval:(int) n{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:n] forKey:@"config_interval"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)config_setChance:(int) n{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:n] forKey:@"config_chance"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)config_setLock:(int) n{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:n] forKey:@"config_lock"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


+(NSDate *)getLastTryTime{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"last_try"];
}

+(void)setLastTryTime{
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"last_try"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
}

+(void)setPass:(NSString *)pass{
    [[NSUserDefaults standardUserDefaults] setObject:pass forKey:@"pass"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
}

+(NSString *)getPass{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"pass"];
}

+(NSTimeInterval)intervalSinceLastTry{
    NSDate *lastTry = [PreferenceManager getLastTryTime];
    if(lastTry == nil){
        return INT_MAX;
    }
    return [[NSDate date] timeIntervalSinceDate:lastTry];
}

+(NSTimeInterval)lockTimeRemaining{
    NSTimeInterval diff = [PreferenceManager intervalSinceLastTry];
    NSTimeInterval lock = [PreferenceManager config_getLock];
    return (lock-diff);
}

+(NSTimeInterval)retryTimeRemaining{
    NSTimeInterval diff = [PreferenceManager intervalSinceLastTry];
    NSTimeInterval inter = [PreferenceManager config_getInterval];
    return (inter-diff);
}



+(NSString *)login:(NSString *)pass
{
    int appLocked = [PreferenceManager appLocked];
    
    if(appLocked == 1)
    {
        //Application locked. Check if the lock period is over
        NSTimeInterval lock_remaining = [PreferenceManager lockTimeRemaining];
        if(lock_remaining > 0)
        {
            return [NSString stringWithFormat:LOCKED, lock_remaining];
        }
        else{
            [PreferenceManager unlockApp];
        }
    }
    else
    {
        NSTimeInterval retry_time_remaining = [PreferenceManager retryTimeRemaining];
        if(retry_time_remaining > 0)
        {
            return [NSString stringWithFormat:EARLY, retry_time_remaining];
        }
        else{
            
        }
    }
    
    [PreferenceManager setLastTryTime];
    
    NSString *challenge = [PreferenceManager getPass];
    if (challenge != nil && [challenge isEqualToString:pass]) {
        [PreferenceManager resetNumTries];
        return nil;
    }
    
    [PreferenceManager increaseNumTries];
    
    int num_tries = [PreferenceManager getNumTries];
    int allowed_tries = [PreferenceManager config_getChance];
    if(num_tries >= allowed_tries){
        [PreferenceManager lockApp];
        [PreferenceManager resetNumTries];
    }
    
    int remaining_trials = allowed_tries - num_tries;
    
    NSString* message = [NSString stringWithFormat:@"Please try again. %d more attempts are allowed.", remaining_trials];
    
    return message;
    
    //return LOGIN_ERROR;
}




@end
